package com.qingchi.server.controller;

import com.qingchi.base.service.TalkService;
import com.socialuni.social.model.common.ResultVO;
import com.qingchi.base.constant.status.ContentStatus;
import com.qingchi.base.dbUtil.UnionIdDbUtil;
import com.qingchi.base.model.talk.TalkDO;
import com.qingchi.base.store.TalkQueryStore;
import com.qingchi.base.repository.talk.TalkRepository;
import com.qingchi.base.model.user.UserDO;
import com.qingchi.base.repository.user.UserRepository;
import com.qingchi.base.service.TalkQueryService;
import com.qingchi.base.modelVO.talk.TalkBO;
import com.qingchi.base.modelVO.user.UserTalkQueryVO;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("talk")
public class TalkQueryController {
    @Resource
    private TalkRepository talkRepository;
    @Resource
    private TalkQueryStore talkQueryStore;
    @Resource
    private TalkService talkService;
    @Resource
    private UserRepository userRepository;
    @Resource
    private TalkQueryService talkQueryService;

    /**
     * 有个初始查询全部的，还有查单个的
     *
     * @param user
     * @param queryVO
     * @return
     */

    @PostMapping("queryUserTalks")
    public ResultVO<List<TalkBO>> queryUserTalks(@RequestBody @Valid @NotNull(message = "参数异常") UserTalkQueryVO queryVO, UserDO user, Integer devId) {
        ResultVO<Integer> userIdResult = UnionIdDbUtil.getUserIdByUid(queryVO.getUserId(), devId, user);
        if (userIdResult.hasError()) {
            return new ResultVO(userIdResult);
        }
        Integer userId = userIdResult.getData();

        Optional<UserDO> userOptional = userRepository.findById(userId);
        if (!userOptional.isPresent()) {
            return new ResultVO<>("不存在的用户");
        }
        UserDO detailUser = userOptional.get();
        ResultVO<List<Integer>> talkIdResult = UnionIdDbUtil.getContentIdsByTalkUnionIds(queryVO.getTalkIds(), devId, user);
        if (talkIdResult.hasError()) {
            return new ResultVO<>(talkIdResult);
        }
        List<Integer> talkIds = talkIdResult.getData();
        if (ObjectUtils.isEmpty(talkIds)) {
            talkIds = Collections.singletonList(0);
        }
        List<TalkDO> talks;
        if (user != null && detailUser.getId().equals(user.getId())) {
            talks = talkService.queryTalksTop10ByMine(talkIds, detailUser.getId());
        } else {
            talks = talkService.queryTalksTop10ByUser(talkIds, detailUser.getId());
        }
        return new ResultVO<>(talks.stream().map(talkDO -> new TalkBO(user, talkDO, devId)).collect(Collectors.toList()));
//        return queryService.queryTalkListByFilter(query);
        /*if (homeType == HomeTypeEnum.FOLLOW.getValue()) {
            //为关注页
            return queryService.queryFollowTalkList();
        } else if (homeType == HomeTypeEnum.MATCH.getValue()) {
            //为匹配页
            return queryService.queryMatchTalkList();
        } else {
            //为全部页
        }*/
    }




    @PostMapping("queryTalkDetail")
    public ResultVO<TalkBO> queryTalkList(@Valid @NotNull String talkId, UserDO user, Integer devId) {
        ResultVO<Integer> talkIdResult = UnionIdDbUtil.getTalkIdByUnionId(talkId, devId, user);
        if (talkIdResult.hasError()) {
            return new ResultVO<>(talkIdResult);
        }
        Integer talkIdInt = talkIdResult.getData();

        Optional<TalkDO> optionalTalkDO = talkRepository.findOneByIdAndStatusIn(talkIdInt, ContentStatus.selfCanSeeContentStatus);
        if (optionalTalkDO.isPresent()) {
            TalkDO talkDO = optionalTalkDO.get();
            return new ResultVO<>(new TalkBO(user, talkDO, true, devId));
        }
        return new ResultVO<>("无法查看不存在的动态");
//        return queryService.queryTalkListByFilter(query);
        /*if (homeType == HomeTypeEnum.FOLLOW.getValue()) {
            //为关注页
            return queryService.queryFollowTalkList();
        } else if (homeType == HomeTypeEnum.MATCH.getValue()) {
            //为匹配页
            return queryService.queryMatchTalkList();
        } else {
            //为全部页
        }*/
    }
}
